package com.irdstudio.biz.rule.service.impl;

import java.util.List;
import java.util.Objects;

import com.irdstudio.biz.rule.common.BizUtil;
import com.irdstudio.biz.rule.dao.domain.SRuleCollParam;
import com.irdstudio.biz.rule.service.facade.SRuleCollParamService;
import com.irdstudio.biz.rule.service.facade.SRuleParamService;
import com.irdstudio.biz.rule.service.vo.SRuleCollParamVO;
import com.irdstudio.biz.rule.service.vo.SRuleParamVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.biz.rule.service.facade.SRuleCollDetailService;
import com.irdstudio.biz.rule.dao.SRuleCollDetailDao;
import com.irdstudio.biz.rule.dao.domain.SRuleCollDetail;
import com.irdstudio.biz.rule.service.vo.SRuleCollDetailVO;
import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 规则集明细				<p>
 * @author AutoCoder
 * @date 2019-10-30
 */
@Service("sRuleCollDetailServiceImpl")
public class SRuleCollDetailServiceImpl implements SRuleCollDetailService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SRuleCollDetailServiceImpl.class);

	@Autowired
	private SRuleCollDetailDao sRuleCollDetailDao;

	@Autowired
	@Qualifier("sRuleParamServiceImpl")
	private SRuleParamService sRuleParamService;


	@Autowired
	@Qualifier("sRuleCollParamServiceImpl")
	private SRuleCollParamService sRuleCollParamService;
	/**
	 * 新增操作
	 */
	public int insertSRuleCollDetail(SRuleCollDetailVO inSRuleCollDetailVo) {
		logger.debug("当前新增数据为:"+ inSRuleCollDetailVo.toString());
		int num = 0;
		try {
			SRuleCollDetail sRuleCollDetail = new SRuleCollDetail();
			beanCopy(inSRuleCollDetailVo, sRuleCollDetail);
			num = sRuleCollDetailDao.insertSRuleCollDetail(sRuleCollDetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SRuleCollDetailVO inSRuleCollDetailVo) {
		logger.debug("当前删除数据条件为:"+ inSRuleCollDetailVo);
		int num = 0;
		try {
			SRuleCollDetail sRuleCollDetail = new SRuleCollDetail();
			beanCopy(inSRuleCollDetailVo, sRuleCollDetail);
			num = sRuleCollDetailDao.deleteByPk(sRuleCollDetail);

			// 删除规则参数配置
			SRuleCollParamVO paramVO = new SRuleCollParamVO();
			paramVO.setDtlId(sRuleCollDetail.getDtlId());
			List<SRuleCollParamVO> paramVOList = sRuleCollParamService.queryAllOwner(paramVO);
			if (CollectionUtils.isNotEmpty(paramVOList)) {
				for (SRuleCollParamVO param : paramVOList) {
					sRuleCollParamService.deleteByPk(param);
				}
			}
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ inSRuleCollDetailVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SRuleCollDetailVO inSRuleCollDetailVo) {
		logger.debug("当前修改数据为:"+ inSRuleCollDetailVo.toString());
		int num = 0;
		try {
			SRuleCollDetail sRuleCollDetail = new SRuleCollDetail();
			beanCopy(inSRuleCollDetailVo, sRuleCollDetail);
			num = sRuleCollDetailDao.updateByPk(sRuleCollDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ inSRuleCollDetailVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public SRuleCollDetailVO queryByPk(SRuleCollDetailVO inSRuleCollDetailVo) {
		
		logger.debug("当前查询参数信息为:"+ inSRuleCollDetailVo);
		try {
			SRuleCollDetail querySRuleCollDetail = new SRuleCollDetail();
			beanCopy(inSRuleCollDetailVo, querySRuleCollDetail);
			SRuleCollDetail queryRslSRuleCollDetail = sRuleCollDetailDao.queryByPk(querySRuleCollDetail);
			if (Objects.nonNull(queryRslSRuleCollDetail)) {
				SRuleCollDetailVO outSRuleCollDetailVo = beanCopy(queryRslSRuleCollDetail, new SRuleCollDetailVO());
				logger.debug("当前查询结果为:"+ outSRuleCollDetailVo.toString());
				return outSRuleCollDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDetailVO> queryAllOwner(SRuleCollDetailVO sRuleCollDetailVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<SRuleCollDetailVO> list = null;
		try {
			if (StringUtils.isNotBlank(sRuleCollDetailVo.getSort())) {
				String sort = BizUtil.transferCamelCaseToHyphen(sRuleCollDetailVo.getSort());
				sRuleCollDetailVo.setSort(sort);
			}
			if (StringUtils.isBlank(sRuleCollDetailVo.getOrder())) {
				sRuleCollDetailVo.setOrder("asc");
			}
			List<SRuleCollDetail> sRuleCollDetails = sRuleCollDetailDao.queryAllOwnerByPage(sRuleCollDetailVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sRuleCollDetails.size());
			pageSet(sRuleCollDetails, sRuleCollDetailVo);
			list = (List<SRuleCollDetailVO>) beansCopy(sRuleCollDetails, SRuleCollDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDetailVO> queryAllCurrOrg(SRuleCollDetailVO sRuleCollDetailVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<SRuleCollDetail> sRuleCollDetails = sRuleCollDetailDao.queryAllCurrOrgByPage(sRuleCollDetailVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+sRuleCollDetails.size());
		List<SRuleCollDetailVO> list = null;
		try {
			pageSet(sRuleCollDetails, sRuleCollDetailVo);
			list = (List<SRuleCollDetailVO>) beansCopy(sRuleCollDetails, SRuleCollDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollDetailVO> queryAllCurrDownOrg(SRuleCollDetailVO sRuleCollDetailVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<SRuleCollDetail> sRuleCollDetails = sRuleCollDetailDao.queryAllCurrDownOrgByPage(sRuleCollDetailVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sRuleCollDetails.size());
		List<SRuleCollDetailVO> list = null;
		try {
			pageSet(sRuleCollDetails, sRuleCollDetailVo);
			list = (List<SRuleCollDetailVO>) beansCopy(sRuleCollDetails, SRuleCollDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public int insertSRuleCollDetailList(List<SRuleCollDetailVO> list) {
		if (CollectionUtils.isEmpty(list)) {
			return 0;
		}
		logger.debug("当前新增数据为:"+ list.toString());
		int num = 0;
		for (SRuleCollDetailVO vo : list) {
			try {
				SRuleCollDetail sRuleCollDetail = new SRuleCollDetail();
				beanCopy(vo, sRuleCollDetail);
				sRuleCollDetail.setDtlId(BizUtil.getUUID("DTL"));
				// 插入参数配置记录
				SRuleParamVO paramVO = new SRuleParamVO();
				paramVO.setRuleId(sRuleCollDetail.getRuleId());
				List<SRuleParamVO> paramVOList = sRuleParamService.queryAllOwner(paramVO);
				if (CollectionUtils.isNotEmpty(paramVOList)) {
					for (SRuleParamVO param : paramVOList) {
						SRuleCollParamVO collParam = new SRuleCollParamVO();
						collParam.setParamId(param.getParamId());
						collParam.setRuleId(param.getRuleId());
						collParam.setDtlId(sRuleCollDetail.getDtlId());
						collParam.setRuleName(sRuleCollDetail.getRuleName());
						collParam.setCreateUser(sRuleCollDetail.getUpdateUser());
						collParam.setCreateTime(sRuleCollDetail.getUpdateTime());
						collParam.setUpdateTime(sRuleCollDetail.getUpdateTime());
						collParam.setUpdateUser(sRuleCollDetail.getUpdateUser());
						sRuleCollParamService.insertSRuleCollParam(collParam);
					}
				}
				num += sRuleCollDetailDao.insertSRuleCollDetail(sRuleCollDetail);
			} catch (Exception e) {
				logger.error("新增数据发生异常!", e);
				num = -1;
				throw e;
			}
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}
}
